<?php

//query_posts('meta_key=prodct_order_number&orderby=meta_value&order=ASC');
//query_posts('p=782');
$this_category = get_category($cat);

$args = array('parent'	=> $this_category -> cat_ID);

$my_childs = get_categories( $args ); 

$my_child_IDs = '';
foreach($my_childs as $chill):
	//$my_child_IDs[] = $chill -> cat_ID;
	$my_child_IDs .= '-'.$chill -> cat_ID.',';
endforeach;

//echo 'my child ids '.$my_child_IDs;

$my_child_IDs .= $this_category -> cat_ID;

$paged = ( !empty($paged) ) ? $paged : $page;

$params = array('cat'	=> $my_child_IDs,
                'posts_per_page' =>  get_option('posts_per_page'),
                'paged' => $paged,
				'meta_key' => 'product_order_number',
				'orderby' => 'meta_value',
				'order' => 'ASC'
			);
				
//query_posts(array('category__not_in' => $my_child_IDs));
query_posts($params);

//checking if current category has any child
$childsCategory = get_categories('child_of='.$this_category -> cat_ID);

//print_r($childsCategory);

?>

<section class="general-wrapper category">

<div class="ad-image"><img src="<?php bloginfo('template_directory')?>/images/620by100-ad.jpg" alt="Image" /></div>
<?php if ( have_posts() ) : ?>
<ul>
<?php  while ( have_posts() ) : the_post();

//fech $link_sub_category;

if(!$childsCategory or $this_category -> slug === 'natural-stones'):
	$url_next = get_permalink();
else:
	$url_next = get_bloginfo('siteurl') . '/category/'.$post -> post_name;
endif;
?>
	<li>
    	 <a href="<?php echo $url_next?>" class="post-thumb"><?php if ( has_post_thumbnail() ) the_post_thumbnail('general'); ?></a>
        <div class="title"><h2><a href="<?php echo $url_next?>" rel="bookmark"><?php the_title(); ?></a></h2></div>
    </li>
	<?php

       endwhile; // end of the loop.
    ?>
</ul>
<?php else:?>

<div class="loop">
        <div id="post-0" class="post hentry error404 not-found clear">
            <h2><?php _e( 'Not found', 'unspoken' ); ?></h2>
            <p><?php _e( 'Apologies, but no results were found for the requested criteria. Perhaps searching will help find a related post.', 'unspoken' ); ?></p>
        </div><!-- #post-0 -->
    </div>
    
<?php endif;?>
<div class="ad-image"><img src="<?php bloginfo('template_directory')?>/images/620by100-ad.jpg" alt="Image" /></div>

</section>
